//
//  Team9s_discussViewController.m
//  Team9Project
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9s_discussViewController.h"
#import "Team9SeeOneViewController.h"
#import "JQAFNetworkTool.h"
#import "Team9s_describeModel.h"
#import "Team9s_discussModel.h"
#import "Team9s_respondModel.h"
#import "Team9s_respondCell.h"
#import "Team9s_aboutDiscussCell.h"
#import "Team9BaseLabel.h"
#import "Team9BaseView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
@interface Team9s_discussViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,strong)  Team9s_describeModel *describeModel;
@property (nonatomic, strong) NSMutableArray *respondArray;
@property (nonatomic, strong) NSMutableArray *aboutDiscussArray;
@property (nonatomic, strong) UITableView *discussTableview;

// 头视图
@property (nonatomic, strong) Team9BaseView *headView;
@property (nonatomic, strong) Team9BaseLabel *titleLabel;
@property (nonatomic, strong) Team9BaseLabel *userNameLabel;
@property (nonatomic, strong) Team9BaseLabel *typeLabel;
@property (nonatomic, strong) UIImageView *respondImageView;
@property (nonatomic, strong) Team9BaseLabel *respondLabel;
@property (nonatomic ,strong) Team9BaseLabel *describeLabel;
@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, strong) Team9BaseView *cutView;

@property (nonatomic, strong) NSMutableArray *picArray;
@end

@implementation Team9s_discussViewController



- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"讨论详情";
    
    
    [self createTableView];
    self.empty = [Team9EmptyTool toolWithScrollView:self.discussTableview];
    [self createData];
    
   }

#pragma mark 数据解析
- (void)createData{
   NSString *str = [NSString stringWithFormat:@"http://api.meilijia.com/app/topic_info_page?topic_id=%@",self.topicID];

 
    [Team9RefreshTool getUrl:str success:^(id result) {
        
        
        // 描述
        self.describeModel = [[Team9s_describeModel alloc] init];
        [self.describeModel setValuesForKeysWithDictionary:result[@"data"][@"topic_info"][@"data"][@"topic_info"]];
        
        [self giveValue];
        [self s_Constraints];
        
        
        // 回应
        self.respondArray = [NSMutableArray array];
        self.respondArray = [Team9s_respondModel baseModelByArr:result[@"data"][@"topic_info"][@"data"][@"reply_list"]].mutableCopy;
        
        // 相关讨论
        self.aboutDiscussArray = [NSMutableArray array];
        self.aboutDiscussArray = [Team9s_discussModel baseModelByArr:result[@"data"][@"topic_rel_topic_list"][@"data"]].mutableCopy;
        
        
        [self.discussTableview reloadData];

        
    } scrollView:self.discussTableview empty:self.empty];
    
    
//        [JQAFNetworkTool getUrl:str body:nil result:JQJSON headerFile:nil success:^(id result) {
//        
//        // 描述
//        self.describeModel = [[Team9s_describeModel alloc] init];
//        [self.describeModel setValuesForKeysWithDictionary:result[@"data"][@"topic_info"][@"data"][@"topic_info"]];
//        
//        [self giveValue];
//        [self s_Constraints];
//        
//        
//        // 回应
//        self.respondArray = [NSMutableArray array];
//        self.respondArray = [Team9s_respondModel baseModelByArr:result[@"data"][@"topic_info"][@"data"][@"reply_list"]].mutableCopy;
//        
//        // 相关讨论
//        self.aboutDiscussArray = [NSMutableArray array];
//        self.aboutDiscussArray = [Team9s_discussModel baseModelByArr:result[@"data"][@"topic_rel_topic_list"][@"data"]].mutableCopy;
//        
//        
//        [self.discussTableview reloadData];
//        
//    } failure:^(NSError *error) {
//        
//        
//    }];

}




- (void) createTableView{

    self.discussTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.discussTableview];
    self.discussTableview.delegate = self;
    self.discussTableview.dataSource =self;
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    self.discussTableview.tableFooterView = view;
    
    
    // 头视图部分
    self.headView = [[Team9BaseView alloc] init];

    
    
    self.titleLabel = [[Team9BaseLabel alloc] init];
    [self.headView addSubview:self.titleLabel];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.font = [UIFont systemFontOfSize:14 / 375.0 * WIDTH];
    self.titleLabel.numberOfLines = 0;
    [self.titleLabel sizeToFit];
  
    
    
    self.userNameLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeOrangeGreen];
    [self.headView addSubview:self.userNameLabel];
    self.userNameLabel.font = [UIFont systemFontOfSize:11 / 375.0 * WIDTH];
    [self.userNameLabel sizeToFit];
    self.userNameLabel.numberOfLines = 0;
    
    self.cutView = [[Team9BaseView alloc] init];
    [self.headView addSubview:self.cutView];
    self.cutView.backgroundColor= [UIColor colorWithWhite:0.885 alpha:1.000];
    
    self.typeLabel = [[Team9BaseLabel alloc] init];
    [self.headView addSubview:self.typeLabel];
    self.typeLabel.backgroundColor = [UIColor clearColor];
    self.typeLabel.font = [UIFont systemFontOfSize:11 / 375.0 * WIDTH];
    
    self.respondImageView = [[UIImageView alloc] init];
    [self.headView addSubview:self.respondImageView];
    self.respondImageView.backgroundColor = [UIColor clearColor];
    
    self.respondLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeBlackWhite];
    [self.headView addSubview:self.respondLabel];
    self.respondLabel.font = [UIFont systemFontOfSize:10 / 375.0 * WIDTH];
    
    self.describeLabel = [[Team9BaseLabel alloc] init];
    self.describeLabel.backgroundColor = [UIColor clearColor];
    [self.headView addSubview:self.describeLabel];
    self.describeLabel.numberOfLines = 0;
    [self.describeLabel sizeToFit];
    self.describeLabel.font = [UIFont systemFontOfSize:13 / 375.0 * WIDTH];
    
    
    self.picImageView = [[UIImageView alloc] init];
    [self.headView addSubview:self.picImageView];
    self.picImageView.backgroundColor = [UIColor clearColor];
    self.picImageView.userInteractionEnabled = YES;  // 打开imageView的用户交互, 添加手势
    
//        self.discussTableview.separatorInset = UIEdgeInsetsMake(500,0, 0, 0);

    
    
    // 设置点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.picImageView addGestureRecognizer:tap];
    
    
}


#pragma mark  点击的方法
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
//    NSLog(@"%@",(NSMutableArray*)self.describeModel.pics);
    if (((NSMutableArray*)self.describeModel.pics).count != 0) {
        
        self.picArray = @[].mutableCopy;
        Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
        [self.picArray addObject:self.describeModel.pics[0][@"pic_580"] ];
         seeVC.picArray = self.picArray;
        
        [self presentViewController:seeVC animated:YES completion:^{
            
            
        }];
    }
    
   
   
    
    
    
    
}

- (void)s_Constraints{
    
    
    
   [self.cutView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.userNameLabel.mas_right).offset(3 / 375.0 * WIDTH);
       make.top.equalTo(self.titleLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
       make.width.mas_equalTo(1 / 375.0 * WIDTH);
       make.height.equalTo(self.userNameLabel.mas_height);
       
   }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cutView.mas_right).offset(3 / 375.0 * WIDTH);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
        make.width.equalTo(self.headView.mas_width).multipliedBy(0.18);
        make.height.mas_equalTo(12 / 375.0 * WIDTH);
        
    }];
    
    [self.respondImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
        make.width.mas_equalTo(12 / 375.0 * WIDTH);
        make.height.mas_equalTo(12 / 375.0 * WIDTH);
        make.right.equalTo(self.respondLabel.mas_left).offset(-3 / 375.0 * WIDTH);
        
    }];
    
    [self.respondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
        make.width.equalTo(self.headView.mas_width).multipliedBy(0.02);
        make.height.equalTo(self.typeLabel.mas_height);
        make.right.equalTo(self.headView.mas_right).offset(-10 / 375.0 * WIDTH);
        
    }];
    
    
    
    
    if (self.describeModel.pics ) {
        [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.describeLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
            make.left.equalTo(self.headView.mas_left).offset(10 / 375.0 * WIDTH);
            make.width.mas_equalTo(90 / 375.0 * WIDTH);
            make.height.mas_equalTo(110 / 375.0 * WIDTH);
            
        }];

    }else{
   
        [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.describeLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
            make.left.equalTo(self.headView.mas_left).offset(10 / 375.0 * WIDTH);
            make.width.mas_equalTo(90 / 375.0 * WIDTH);
            make.height.equalTo(@0);
            
        }];

    }
    
    
    
}


- (void)giveValue{
    
    self.titleLabel.text = self.describeModel.title;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView.mas_left).offset(10 / 375.0 * WIDTH);
        make.top.equalTo(self.headView.mas_top).offset(10 / 375.0 * WIDTH);
        make.right.equalTo(self.headView).offset(-10 / 375.0 * WIDTH);
    }];
   CGFloat titleHeight = [Team9Tool heightByFloatSize:14 / 375.0 * WIDTH width:WIDTH - 20 / 375.0 * WIDTH With:self.describeModel.title];

    
    self.userNameLabel.text = self.describeModel.author_nick;
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11 / 375.0 * WIDTH], NSFontAttributeName, nil];
    CGRect rect = [self.userNameLabel.text boundingRectWithSize:CGSizeMake(0, 12 / 375.0 * WIDTH) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView.mas_left).offset(10 / 375.0 * WIDTH);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
        make.width.equalTo(@(rect.size.width));
        make.height.mas_equalTo(12 / 375.0 * WIDTH);
        
    }];
    

    
    self.typeLabel.text = self.describeModel.class_name;
    
    self.respondImageView.image = [UIImage imageNamed:@"iconfont-message"];
    self.respondLabel.text = self.describeModel.reply_count;
    
    self.describeLabel.text = self.describeModel.content;
    [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView.mas_left).offset(10 / 375.0 * WIDTH);
        make.top.equalTo(self.typeLabel.mas_bottom).offset(5 / 375.0 * WIDTH);
        make.right.equalTo(self.headView.mas_right).offset(-10 / 375.0 * WIDTH);
    }];
   CGFloat describeLabelHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH width:WIDTH - 20 / 375.0 * WIDTH With:self.describeModel.content];
    
    if (self.describeModel.pics.count) {
        [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.describeModel.pics[0][@"pic"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [UIView animateWithDuration:0.3 animations:^{
                CATransition *transition = [CATransition animation];
                transition.type = @"fade";
                [transition setDuration:0.5];
                [transition setRepeatCount:1];
                // 向imageView上添加动画效果
                [self.picImageView.layer addAnimation:transition forKey:@"transition"];
            }];
        }];
    }
    
    
    self.headView.frame = CGRectMake(0, 0, WIDTH, titleHeight + describeLabelHeight + 150 / 375.0 * WIDTH);
    self.discussTableview.tableHeaderView = self.headView;

}

#pragma mark tableview协议

// 分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

// 分区高度
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        if (self.respondArray) {
            return HEGHT * 0.055;
        }else{
            return 0;
        }
    }else{
        
        if (self.aboutDiscussArray) {
            return HEGHT * 0.055;
        }else{
            return 0;
        }
    
    }
    

}
// 分区标题内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    Team9BaseView *sectionView = [[Team9BaseView alloc] initWithType:ViewTYpeLight];
    sectionView.frame = CGRectMake(0, 0, WIDTH, HEGHT*0.055);
  
    
    Team9BaseView *leftView = [[Team9BaseView alloc] initWithFrame:CGRectMake(10 / 375.0 * WIDTH, 13 / 375.0 * WIDTH, 8 / 375.0 * WIDTH, 15 / 375.0 * WIDTH)];
    leftView.backgroundColor = [UIColor orangeColor];
    [sectionView addSubview:leftView];
    
    
    Team9BaseLabel *titleLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeBlackWhite];
    titleLabel.font = [UIFont systemFontOfSize:15 / 375.0 * WIDTH];
    [titleLabel sizeToFit];
    titleLabel.numberOfLines = 0;
    [sectionView addSubview:titleLabel];
    if (section == 0) {
        titleLabel.text = @"回应";
    }else{
    titleLabel.text = @"相关讨论";
    }
    NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 / 375.0 * WIDTH], NSFontAttributeName, nil];
    CGRect rect = [titleLabel.text boundingRectWithSize:CGSizeMake(0, 23 / 375.0 * WIDTH) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    titleLabel.frame = CGRectMake(23 / 375.0 * WIDTH, 8 / 375.0 * WIDTH, rect.size.width, 23 / 375.0 * WIDTH);
    
    
    Team9BaseView *lineView = [[Team9BaseView alloc] init];
    lineView.backgroundColor = [UIColor colorWithWhite:0.592 alpha:1.000];
    [sectionView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleLabel.mas_right).offset(5 / 375.0 * WIDTH);
        make.top.mas_equalTo(sectionView.mas_top).offset(18 / 375.0 * WIDTH);
        make.right.mas_equalTo(sectionView.mas_right).offset(-10 / 375.0 * WIDTH);
        make.height.mas_equalTo(2 / 375.0 * WIDTH);
        
    }];
    
    
    return sectionView;
    
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.respondArray != nil || self.aboutDiscussArray != nil) {
        
        
        if (indexPath.section == 0 ) {
            
            Team9s_respondModel *respondModel = self.respondArray[indexPath.row];
            CGFloat contentHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH width:WIDTH - 20 / 375.0 * WIDTH With:respondModel.content];
            
            return contentHeight + 100 / 375.0 * WIDTH;
        }else if(indexPath.section == 1){
            
            Team9s_discussModel *dissModel = self.aboutDiscussArray[indexPath.row];
            CGFloat contentHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH width:WIDTH - 20 / 375.0 * WIDTH With:dissModel.title];
            
            if (dissModel.pics != nil) {
                return contentHeight + 150 / 375.0 * WIDTH;
            }else{
                return contentHeight + 50 / 375.0 * WIDTH;
            }
            
        }

    }else{
    
        return 0;
    }
    
    return 0;
    
    
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.respondArray.count;
    }else{

        return self.aboutDiscussArray.count;

    
    
    }
    

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuse";
        Team9s_respondCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell= [[Team9s_respondCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        cell.backgroundColor = [UIColor whiteColor];
        cell.vc = self;
        Team9s_respondModel *respondmodel = self.respondArray[indexPath.row];
        cell.respondModel = respondmodel;
        
        
        return  cell;
    }else{
        
        static NSString *reuse = @"Reuse";
        Team9s_aboutDiscussCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell= [[Team9s_aboutDiscussCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        Team9s_discussModel *discussModel = self.aboutDiscussArray[indexPath.row];
        cell.dissModel = discussModel;
        
        
        __weak __typeof(self)weakSelf = self;
        void (^Block)(NSInteger ) = ^(NSInteger a){
            if (a == 1000 && discussModel.pics.count > 0) {
                
                self.picArray = @[].mutableCopy;
                
                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
                  Team9s_discussModel *discussModel = self.aboutDiscussArray[indexPath.row];
                
                for (NSInteger con = 0; con < discussModel.pics.count; con ++) {
                    
                    [self.picArray addObject:discussModel.pics[con][@"pic"]];
                   
                    
                }
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 0;
                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
                
            }else if (a == 1001 && discussModel.pics.count > 1){
                
                self.picArray = @[].mutableCopy;

                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
                
                Team9s_discussModel *discussModel = self.aboutDiscussArray[indexPath.row];
                
                for (NSInteger con = 0; con < discussModel.pics.count; con ++) {
                    
                    [self.picArray addObject:discussModel.pics[con][@"pic"]];
//                    NSLog(@"图片地址: %@", discussModel.pics[con][@"pic"]);
                    
                }
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 1;
                
                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
                
                
            }else if (a == 1002 && discussModel.pics.count > 2){
                
                self.picArray = @[].mutableCopy;
            
                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
                Team9s_discussModel *discussModel = self.aboutDiscussArray[indexPath.row];
                
                for (NSInteger con = 0; con < discussModel.pics.count; con ++) {
                    
                    [self.picArray addObject:discussModel.pics[con][@"pic"]];
                    
                }
                
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 2;
                
                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
               
                
            }else if (a == 1003 && discussModel.pics.count > 3){
               
                self.picArray = @[].mutableCopy;
           
                Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
                
                Team9s_discussModel *discussModel = self.aboutDiscussArray[indexPath.row];
                
                for (NSInteger con = 0; con < discussModel.pics.count; con ++) {
                    
                    [self.picArray addObject:discussModel.pics[con][@"pic"]];
                    
                }
                seeVC.picArray = [NSArray arrayWithArray:self.picArray];
                seeVC.number = 3;

                [weakSelf presentViewController:seeVC animated:YES completion:^{
                    
                    
                }];
                
                
            }
            
        };
        cell.block = Block;
        
        
        return  cell;

    }

    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (indexPath.section == 1) {
        Team9s_discussViewController *dissVC = [[Team9s_discussViewController alloc] init];
        Team9s_discussModel *disModel = self.aboutDiscussArray[indexPath.row];
        dissVC.topicID = disModel.topic_id;
        
        [self.navigationController pushViewController:dissVC animated:YES];
    }
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
